package com.hollycrm.hollysqm.util;

import lombok.extern.log4j.Log4j;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * Created by zhaihw on 2016/5/4.
 * 配置文件工具
 */
@Log4j
public class ConfigUtil {
    /**
     * 私有化构造
     */
    private ConfigUtil() {
    }
    /**
     * 缓存起来吧
     */
    private static Map<String,String> configMap;
    static {
        configMap = new HashMap<>();
        init();
    }
    /**
     * 配置文件在哪里？
     */
    private static final String fileName = "/analyzer.properties";


    /**
     * 读取配置文件
     */
    public static void init() {
        try {
            Properties pros = new Properties();
            pros.load(ConfigUtil.class.getResourceAsStream(fileName));
            Iterator<Map.Entry<Object, Object>> its = pros.entrySet().iterator();
            configMap.clear();
            while (its.hasNext()) {
                Map.Entry<Object, Object> entry = its.next();
                String key = (String) entry.getKey();
                String val = (String) entry.getValue();
                configMap.put(key, val);
                log.info(key + " = " + val);
            }
        } catch (Exception e) {
            log.error("load config has error", e);
        }
    }

    /**
     * 获取val
     * @param key key
     * @return val
     */
    public static String getProp(String key) {
        return configMap.get(key);
    }
}
